mui.init({
	pullRefresh: {
		container: '#pullrefresh',
		down: {
			callback: pulldownRefresh
		},
		up: {
			contentrefresh: '正在加载...', //正在加载...
			callback: pullupRefresh
		}
	}
});

// 服务器地址
//var G_WEBBASE = "http://106.14.139.5:7014/GZ/";

// 获取用户/医生 头像
var imgUrl = 'http://106.14.139.5:7014/GZ/images/getimages?guid=';

var caseGuid;

var arryUsr = {}; // 存储病人信息
var arryPtype = {}; // 存储病人的就诊记录
var count = 1; // 翻页
var totalNumber; //总条数

var usrGuid = GetLocationParam("usrGuid"); // 用户GUID
//var usrGuid = "591D34C65FDBC7FCE050007F01005BDA"; // 测试数据 AAA9E0A9B207581AE040007F010063F7

$(function() {

	jiaohu(); // 获取用户信息及病历详情
	// 患者信息

	document.getElementById('headImg').src = imgUrl + usrGuid;

	$('.name').text(arryUsr.name); //姓名

	if(arryUsr.sex == "1") {
		$('.sex').text("男"); // 性别
	} else if(arryUsr.sex == "2") {
		$('.sex').text("女"); // 性别
	} else {
		$('.sex').text(arryUsr.sex); // 性别
	}

	$('.age').text(arryUsr.age); // 年龄
	$('.iphone').text(arryUsr.mobile); // 手机号
	$('.identity').text(arryUsr.IDCARD); // 身份证
	$('.ybcard').text(arryUsr.MEDICARECARD); // 医保卡号

})

/**
 * 页面数据AJAX请求
 */	
function jiaohu() {
	//
	var webUrl = XHDOC_WEBBASE + "dtcasehistory/getparticulars?";
	webUrl += "pageNo=1&pageSize=10&sickguid=" + usrGuid;
//		console.log(webUrl);

	try {
		$.ajax({
			url: webUrl,
			type: "POST",
			async: false,
			dataType: "json",
			success: function(result) {
				totalNumber = Math.ceil(result.data.totalRecord / 10); //总条数

				try {

					if(result.code == "000") {

						arryUsr = result.data["usr"]; // 患者信息
						usrGuid = arryUsr.guid; // 获取患者GUID
						arryPtype = result.data["parameterType"]; // 用户就诊记录信息

						usrGuid = arryUsr.guid; // 存储用户GUID

						if(arryPtype == "") {
							setTimeout("$('.without').show()", 1000);
						}

					} else if(result.code == "001") {
						mui.toast('请求出错');
					}

				} catch(e) {
					mui.toast('获取数据失败' + e);
				}

			},
			error: function(xhr, status, error) {
				if(status == "error")
					mui.toast('请检查网络链接' + error);
			}

		});

	} catch(e) {

		mui.toast("请检查网络连接" + e);

	}

}

/**
 * 下拉刷新具体业务实现
 */
function pulldownRefresh() {
	jiaohu();
	setTimeout(function() {
		var table = document.body.querySelector('.mui-table-view');
		var cells = document.body.querySelectorAll('.mui-table-view-cell');

		for(var i = 0; i < arryPtype.length; i++) {

			var li = document.createElement('li');
			li.className = 'mui-table-view-cell mui-media case';
			li.innerHTML =
				'<a href="javascript:;">' +
				'<img src="' + imgUrl + arryPtype[i].docguid + '" />' +
				'<p class="docname">' + arryPtype[i].doc + '</p>' +
				'<p class="time">' + arryPtype[i].wdat + '</p>' +
				'<span>查看病历</span>' +
				'</a>';

		}
		mui('#pullrefresh').pullRefresh().endPulldownToRefresh();
	}, 1500);

}

/**
 * 上拉加载具体业务实现
 */
function pullupRefresh() {
	jiaohu();
	setTimeout(function() {
		mui('#pullrefresh').pullRefresh().endPullupToRefresh((++count >= totalNumber + 1)); //参数为true代表没有更多数据了。
		var table = document.body.querySelector('.mui-table-view');
		var cells = document.body.querySelectorAll('.mui-table-view-cell');
		for(var i = 0; i < arryPtype.length; i++) {

			caseGuid = arryPtype[i].guid;
			var li = document.createElement('li');
			li.className = 'mui-table-view-cell mui-media case';
			var imgurl = 'https://sch.xmidware.com/chis/EAFormBlob.sp?guid=';
			li.innerHTML =
				'<a href="javascript:;" id="case' + i + '" data-guid="' + caseGuid + '">' +
				'<img src="' + imgUrl + arryPtype[i].docguid + '" />' +
				'<p class="docname">' + arryPtype[i].doc + '</p>' +
				'<p class="time">' + arryPtype[i].wdat + '</p>' +
				'<span>查看病历</span>' +
				'</a>';

			table.appendChild(li);
		}
	}, 1500);
}

if(mui.os.plus) {
	mui.plusReady(function() {
		setTimeout(function() {
			mui('#pullrefresh').pullRefresh().pullupLoading();
		}, 1000);

	});
} else {
	mui.ready(function() {
		mui('#pullrefresh').pullRefresh().pullupLoading();
	});
}

/**
 * 免费咨询医生点击事件
 */

mui("body").on("tap", ".case", function() {
	var caseName = this.id;
	caseName = document.getElementById(caseName);
	/*console.log(this.nodeName);*/

	window.android.showpageDocBrief(caseGuid);
});

/**
 * 打开病历页面
 * 病历ID：caseGuid 
 * 打开页面：view_medical_record.html
 */
function showpageDocBrief(caseGuid) {

	var page = "view_medical_record.html?caseGuid=" + caseGuid;
	var pageId = "view_medical_record";

	mui.openWindow({
		url: page,
		id: pageId,
		preload: true,
		show: {
			aniShow: 'pop-in'
		},
		styles: {
			popGesture: 'hide'
		}
	});

}

//返回箭头事件
mui.back = function() {

	console.log('进入返回事件...');

	try {

		if(G_BROWSETYPE == 'IOS') {

			//调用ios的返回事件

		} else if(G_BROWSETYPE == 'ANDROID') {

			console.log('进入安卓判断fromtips值为:' + fromtips);

			if(fromtips == "first") {
				window.history.go(-1);
			} else {
				location.href = 'health-record.html?b=1';
			}

		}

	} catch(e) {
		console.log(e);
	}

}